﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Modules_user_AddNew : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
        this.btnCancel.Attributes.Add("onclick","return ClearAll()");

        if (!this.IsPostBack) {
            ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
        }
    }

    

    private void AddNew() {
        String strMess = "";
        int flag = -1;
            try
            {
                WebData db = new WebData();
                if (db.tbl_Users.GetAll().FindAll(p => p.NameUser.ToLower() == txtnameUser.Text.Trim().ToLower()).Count > 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('User này đã tồn tại');", true);
                    return;
                }
                DataLibrary.tbl_User item = new DataLibrary.tbl_User();
                item.NameUser = txtnameUser.Text;
                item.EmailUser= txtemailUser.Text;
                String strPass = db.EncodeMD5(txtpasswordUser.Text.Trim());
                item.PasswordUser = strPass;
                item.LastLoginUser = DateTime.Now;
                flag = db.tbl_Users.InsertOnSubmit(item);
                strMess = Message.Insert_err;
            }
            catch (Exception err)
            {
                strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
            }
            if (flag > 0)
            {
                Response.Redirect(ViewState["URLReferrer"].ToString());
            }
            else
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + strMess + "');", true);
        
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        AddNew();

    }
}